// component.ts
import { Component, OnInit, ViewChild } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { JsonApiService } from 'app/core/api/json-api.service'
import { config } from '../../shared/smartadmin.config'

declare var $: any
declare var Viewer: any
@Component({
  selector: 'sa-device-sign-for',
  templateUrl: './device-sign-for.component.html',
  styleUrls: ['./device-sign-for.css']
})
export class DeviceSignForComponent implements OnInit {
  tableData: any = [] // table的表单数据
  totalCounts: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  activeSelected: string = '' //row高亮
  // 查询参数
  selectedCompanyId: any = '' // 公司的id
  selectedCompanyName: any = '' // 公司的名称
  startTime: any = '' // 开始时间
  endTime: any = '' // 结束时间
  searchKey: any = '' // 用户的搜索关键字

  constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }
  ngOnInit() {
    this.load()
  }

  // 时间选择回调
  dateSelect(time) {
    this.startTime = time.startTime
    this.endTime = time.endTime
  }
  

  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
  }

  // 获取列表数据
  load() {
    let param = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: this.startTime,
        endTime: this.endTime,
        searchKey: common.stringTrim(this.searchKey)
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(param)
    this.jsonApiService.http('deviceSendBack/getDeviceSendBackPage.html', reqdata).subscribe(result => {
      if (result.isSuccess && result.exhibitDatas) {
        this.tableData = result.exhibitDatas
        this.totalCounts = result.recordCount
      }
    })
  }
  // 删除
  rowDelete(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      this.jsonApiService.http('deviceSendBack/deleteDeviceSendBackById.html', 'id=' + row.id).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 分页回调
  paginate(e) {
    this.activeSelected = ''
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  // 刷新表
  refresh() {
    this.load()
  }
  // 表单查询
  tableSearch() {
    this.curPage = 1
    this.load()
  }

  // 数据导出
	exportData() {
		let param = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: this.startTime,
        endTime: this.endTime,
        searchKey: common.stringTrim(this.searchKey),
        export: '1'
      }
    }
    let timeCheck = common.correctTime(param.parameters, ['export'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(param, "gridPager", config.BaseURL + "deviceSendBack/getDeviceSendBackPage.html")
	}

  /**
   * 图片查看的方法
   */
  imgList: any = []
  imgLocalList: any = []
  isViewer2: boolean = false //默认隐藏
  //显示图片组件
  viewer2: any = ''
  showImg2(row, modal) {
    this.imgList = []
    this.imgLocalList = []
    this.imgList = JSON.parse(row.receiptPicture)

    let localImg
    for (var i = 0; i < this.imgList.length; i++) {
      if (this.imgList[i].indexOf('http') == -1) {
        if (this.imgList[i][0] == '/') {
          localImg = config.BaseURL + this.imgList[i].substr(1)
        } else {
          localImg = config.BaseURL + this.imgList[i]
        }
      } else {
        localImg = this.imgList[i]
      }
      this.imgLocalList.push(localImg)
    }
    modal.show()

    if (this.isViewer2 && $('.viewer-container').length > 0) {
      this.viewer2.destroy()
    }
  }
  // 关闭图片查看
  closeImgModal2(modal) {
    modal.hide()
    this.imgList = []
    this.imgLocalList = []
  }
  //图片放大
  bigimg(type, row) {
    this.showBigImg(type)
  }
  showBigImg(type) {
    var that = this
    var imgList
    if (type == 'Modal2') {
      imgList = $('.imagesInfo2')[0]
      this.viewer2 = new Viewer(imgList, {
        url: 'src',
        title: function (image) {
          return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')'
        }
      })
      that.isViewer2 = true
    }
  }
}
